﻿#region 命名空间

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WinnerCMS.BLL.PageFactory.ManageHtml;
using WinnerCMS.Common;
using WinnerCMS.Data;
using WinnerCMS.Model;
using WinnerCMS.Common.Security;

#endregion

namespace WinnerCMS.BLL
{
    /// <summary>
    /// 评论管理
    /// </summary>
    public class CommentManage : MarshalByRefObject
    {
        private static readonly CommentDal Dal = new CommentDal();

        #region 绑定分页数据

        /// <summary>
        /// 绑定分页数据
        /// </summary>
        /// <param name="contentId"></param>
        /// <param name="pass"></param>
        /// <param name="pp"></param>
        /// <returns></returns>
        public IList<Comment> BindList(int contentId, bool pass, PagingParam pp)
        {
            return Dal.BindList(contentId, pass, pp);
        }

        #endregion

        #region 删除评论

        /// <summary>
        /// 删除评论
        /// </summary>
        /// <param name="ids"></param>
        [Logging("删除ID：{@p0} 的评论"), ExceptionCall]
        public Result Delete(string ids)
        {
            Result res = new Result();
            if (string.IsNullOrEmpty(ids) == false)
            {
                int[] ints = ids.ToArrInt();
                Dal.Delete(ints); //执行删除

                foreach (int id in ints)
                {
                    DeleteHTML.ContentPage(id);
                    MakeSinglePage.ContentPageHTML(id);
                }
            }
            else
            {
                res.Errors = "至少选择一个删除目标！";
            }
            return res;
        }

        #endregion

        #region 删除评论

        /// <summary>
        /// 删除评论
        /// </summary>
        /// <param name="ip"></param>
        [Logging("删除IP：{@p0} 的所有评论"), ExceptionCall]
        public Result DeleteForIP(string ip)
        {
            Result res = new Result();
            if (string.IsNullOrEmpty(ip) == false)
            {
                IList<int> list = Dal.DeleteForIP(ip);

                foreach (int id in list)
                {
                    DeleteHTML.ContentPage(id);
                    MakeSinglePage.ContentPageHTML(id);
                }
            }
            else
            {
                res.Errors = "至少选择一个删除目标！";
            }
            return res;
        }

        #endregion

        #region 审核评论

        /// <summary>
        /// 审核评论
        /// </summary>
        /// <param name="ids"></param>
        [Logging("通过审核：{@p0}"), ExceptionCall]
        public Result Pass(string ids)
        {
            Result res = new Result();
            if (string.IsNullOrEmpty(ids) == false)
            {
                int[] ints =
                    (from x in ids.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries) select int.Parse(x))
                        .ToArray();
                Dal.Pass(ints);

                foreach (int id in ints)
                {
                    DeleteHTML.ContentPage(id);
                    MakeSinglePage.ContentPageHTML(id);
                }
            }
            else
            {
                res.Errors = "至少选择一个删除目标！";
            }
            return res;
        }

        #endregion

        #region 获取自己和上级

        /// <summary>
        /// 获取自己和上级
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public IList<Comment> GetFatherAndMe(string path)
        {
            return Dal.GetFatherAndMe(path);
        }

        #endregion

        #region 添加评论

        /// <summary>
        /// 添加评论
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public Result Add(Comment model)
        {
            Result resu = new Result();
            CMSConfig config = CMSConfig.Instance();
            if (config.DefendPROC.CheckPost && PostData.PostFormRight() == false)
            {
                resu.Errors = "非法数据来源。";
                SecurityCenterManage.AppErr("发送添加留言数据来源非法。");
                return resu;
            }
            model.Content = HttpUtility.UrlDecode(model.Content);
            if (model.Content.IsNullOrEmpty())
            {
                resu.Errors = "对不起！评论内容不能为空！";
                return resu;
            }
            if (model.Content.Length < 2) //少于2个汉字当作垃圾处理
            {
                resu.Errors = "对不起！评论内容太少！";
                return resu;
            }
            if (model.Content.Length > 300)
            {
                resu.Errors = "对不起！评论不需要搞长篇大论！";
                return resu;
            }

            if (!WordsManage.IsContainChinese(model.Content))
            {
                resu.Errors = "请认真写评论！";
                return resu;
            }

            if (WordsManage.IsContainBanWords(model.Content))
            {
                resu.Errors = "警告：系统判断你可能在发送广告！";
                return resu;
            }
            if (WordsManage.IsDataTooMuch("WinnerCMS_Comment"))
            {
                resu.Errors = "警告：系统判断你可能在发送垃圾广告，因此你的IP已经被加入黑名单，如果你认为这是误判，请联系系统管理员解封！";
                return resu;
            }

            dynamic ma = new CoreManage().GetBaseInfo(model.ContentId);

            if (ma == null)
            {
                resu.Errors = "对应的内容不存在，无法发表评论！";
                return resu;
            }
            if (ma.AllowComment == 0)
            {
                resu.Errors = "评论已经关闭！";
                return resu;
            }
            model.Content = Encoder.HtmlEncode(model.Content);
            model.Content = WordsManage.FilterBadWords(model.Content); //过滤不文明用语
            model.UserName = Encoder.HtmlEncode(model.UserName);

            var mm = ModelManage.GetInfo(model.ModelId);
            model.Pass = mm.CommentAutoPass;
            model.CreateTime = DateTime.Now;
            model.UpdateTime = DateTime.Now;

            Dal.Add(model);

            new CoreManage().UpdateCommentInfo(model.ContentId);

            if (model.Pass)
            {
                MakeSinglePage.ContentPageHTML(model.ContentId);
            }

            return resu;
        }

        #endregion

        #region 显示评论－－前台内容页使用

        /// <summary>
        /// 显示评论－－前台内容页使用
        /// </summary>
        /// <param name="top"></param>
        /// <param name="contentId"></param>
        /// <returns></returns>
        public IList<Comment> CommentShow(int top, int contentId)
        {
            return Dal.GetList(top, contentId);
        }

        #endregion

        #region 本内容下最热评论

        /// <summary>
        /// 本内容下最热评论
        /// </summary>
        /// <param name="contentId"></param>
        /// <returns></returns>
        public IList<Comment> HotComments(int contentId)
        {
            return Dal.HotComments(contentId);
        }

        #endregion

        #region 支持或者反对

        /// <summary>
        /// 支持或者反对
        /// </summary>
        /// <param name="id"></param>
        /// <param name="support"></param>
        public Comment SupportOrOppose(int id, bool support)
        {
            return Dal.SupportOrOppose(id, support);
        }

        #endregion
    }
}